<?php
namespace app\index\controller;

class Question extends Base{
	// 问答首页
	public function index(){
		$this->add_step('问答');

		// 查询条件
		$where       = array();
		// URL参数
		$url_params  = array();
		// 默认
		$is_default  = 1;
		// 默认排序
		$order       = 'order_id,answer_time desc,ask_id desc';
		// 类别ID
		$category_id = $this->_get('category_id');
		if($category_id){
			$where['category_id'] = $category_id;
		}
		// 热门
		$is_hot = $this->_get('is_hot');
		if($is_hot){
			unset($order);
			$order['answer_num']  = 'desc';
			$order['view_num']    = 'desc';
			$where['answer_num']  = array('gt', 0);
			$url_params['is_hot'] = 1;
			$is_default = 0;
		}
		// 未回答
		$unanswer = $this->_get('unanswer');
		if($unanswer){
			$where['answer_num'] = 0;
			$url_params['unanswer'] = 1;
			$is_default = 0;
		}
		$qwhere = $where;
		unset($qwhere['answer_num']);

		// 设置URL参数，用于分页
		$query  = array_merge($qwhere, $url_params);
		// var_dump($qwhere);exit;

		$where['is_delete'] = 0;
		$where['status']    = 1;
		$list  = model('Ask')->where($where)->order($order)->paginate(10, false, ['query' => $query]);

		$this->assign('list', $list);
		$this->assign('qwhere', $qwhere);
		$this->assign('url_params', $url_params);
		$this->assign('query', $query);
		$this->assign('is_default', $is_default);
		$this->assign('category_list', model('Category')->getCategoryList('ask_index'));
		$this->assign('user_active_list', model('User')->get_active_list());
		$this->assign('page', $list->render());
		return $this->view();
	}
	// 提交问题
	public function submit(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$this->add_step('提交问题');
		if($this->isPost()){
			$model = model('Ask');
			$post_data = input('post.');
			$post_data['user_id']     = $this->_user_id;
			$post_data['create_time'] = time();
			// 验证器
			$validate = validate('Ask');
			if($validate && !$validate->scene('add')->check($post_data)){
				$this->error($validate->getError());
			}
			if($model->validate(true)->create($post_data, true) !== false){
				$ask_id = $model->getLastInsID();
				// 悬赏
				model('UserAccount')->ask_submit($this->_user_id, $post_data['reward'], $ask_id);
				// 更新用户问答数量
				model('UserData')->update_user_data($this->_user_id, 'question_num');
				$this->success('发布成功', url('Question/detail', array('ask_id' => $ask_id)));
			} else {
				$this->error('添加失败！');
			}
		}
		$reward_data = model('Config')->_get('reward_val_list', 'reward');
		$reward_list = explode(PHP_EOL, $reward_data);
		$reward_usable = model('UserAccount')->get_account_money($this->_user_id, 'beans_usable');

		$this->assign('reward_list', $reward_list);
		$this->assign('reward_usable', $reward_usable);
		return $this->view();
	}
	// 修改问题
	public function edit(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$ask_id = $this->_get('ask_id', '对不起，您的操作有误！');

		$ask_model = model('Ask');
		$info = $ask_model->find($ask_id);
		// 设置标题
		$this->add_step($info['title']);
		if($info['user_id'] != $this->_user_id || !$info){
			$this->error('对不起，您的操作有误！');
		}
		if($info['is_set_best']){
			$this->error('问题已解决，无法修改！');
		}
		if($this->isPost()){
			$model = model('Ask');
			$post_data = input('post.');
			$post_data['is_anonymous'] = $post_data['is_anonymous'] == 1 ? 1 : 0;
			$post_data['user_id']      = $this->_user_id;
			$post_data['edit_time']    = time();
			// 验证器
			$validate = validate('Ask');
			if($validate && !$validate->scene('save')->check($post_data)){
				$this->error($validate->getError());
			}
			// 追加悬赏
			$reward = $post_data['reward'];
			$post_data['reward'] = $info['reward'] + $reward;
			if($model->validate(true)->update($post_data, array('ask_id' => $ask_id), true) !== false){
				// 追加悬赏账户处理
				if($post_data['reward']){
					model('UserAccount')->ask_submit($this->_user_id, $reward, $ask_id);
				}
				$this->success('修改成功', url('Question/detail', array('ask_id' => $ask_id)));
			} else {
				$this->error('修改失败！');
			}
		}

		$reward_data = model('Config')->_get('reward_val_list', 'reward');
		$reward_list = explode(PHP_EOL, $reward_data);
		$reward_usable = model('UserAccount')->get_account_money($this->_user_id, 'reward_usable');

		$this->assign('info', $info);
		$this->assign('reward_list', $reward_list);
		$this->assign('reward_usable', $reward_usable);
		return $this->view('submit');
	}
	// 问题详情
	public function detail(){
		$ask_id = $this->_get('ask_id', '对不起，您的操作有误！');

		$ask_model = model('Ask');
		$info = $ask_model->find($ask_id);
		if(!$info || !$info['status'] || $info['is_delete']){
			$this->error('问题不存在！');
		}
		// 设置标题
		$this->add_step($info['title']);

		$this->get_answer_list($ask_id);
		$this->assign('best', model("AskAnswer")->where(array('answer_id' => $info['answer_id']))->find());
		$this->assign('info', $info);
		$this->assign('is_fav', model('User')->is_fav($this->_user_id, $ask_id, 'ask_fav'));
		$this->assign('is_focus', model('User')->is_fav($this->_user_id, $ask_id, 'ask_focus'));
		return $this->view();
	}
	

	// 回答列表
	public function get_answer_list($ask_id){
		$ask_id = $ask_id ? $ask_id : $this->_get('ask_id', '对不起，您的操作有误！');
		$model  = model('AskAnswer');
		$pagesize = 10;
		$where['ask_id']    = $ask_id;
		$where['is_best']   = 0;
		$where['is_delete'] = 0;
		$order = 'answer_id desc';
		$list  = $model->where($where)->order($order)->paginate($pagesize, false);

		$this->assign('list', $list);
		$this->assign('page', $list->render());

		return $this->view();
	}
	// 答案收藏
	public function ask_fav(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$ask_id = $this->_get('ask_id', '对不起，您的操作有误！');

		$model = model('User');
		$reid  = $model->set_fav('Ask', 'fav_num', 'ask_id', $this->_user_id, $ask_id, 'ask_fav');
		if($reid == 1){
			// 消息通知
			model('Message')->ask_push($ask_id, 'fav', $this->_user_id);
		}
		if($reid == 2){
			// 消息通知
			model('Message')->ask_push($ask_id, 'fav_cancel', $this->_user_id);
		}

		$data['node_id'] = 'ask-id-' . $ask_id;
		$data['node_data']['fav_num'] = $model->_total_fav_num;
		
		$this->success(model('User')->tips_info, '', $data);
	}
	// 答案收藏
	public function ask_focus(){
		// 设置需要登录才能访问
		$this->user_id(true);
		$ask_id = $this->_get('ask_id', '对不起，您的操作有误！');

		$model = model('User');
		$reid  = $model->set_fav('Ask', 'focus_num', 'ask_id', $this->_user_id, $ask_id, 'ask_focus');
		
		$data['node_id'] = 'ask-id-' . $ask_id;
		$data['node_data']['focus_num'] = $model->_total_fav_num;
		if($reid === false){
			$tips_info = model('User')->tips_info;
		}
		if($reid == 1){
			$tips_info = '关注成功！';
			model('UserData')->update_user_data($this->_user_id, 'focus_question_num');
			// 消息通知
			model('Message')->ask_push($ask_id, 'focus', $this->_user_id);
		}
		if($reid == 2){
			$tips_info = '取消关注成功！';
			model('UserData')->update_user_data($this->_user_id, 'focus_question_num', -1);
			// 消息通知
			model('Message')->ask_push($ask_id, 'focus_cancel', $this->_user_id);
		}
		
		$this->success($tips_info, '', $data);
	}

	//搜索
	public function ajax_search(){

		if($this->isPost()){
			$title = $this->_post('title');
			$title = str_replace('%', '', $title);
			if(!$title){$this->error('标题不能为空');}
			$where['title'] = ['like', '%' . $title . '%'];

			$order['answer_num']  = 'desc';
			$order['view_num']    = 'desc';

			$where['is_delete'] = 0;
			$where['status']    = 1;
			$list = model('Ask')->where($where)->order($order)->select();
			$arr  = [];
			foreach ($list as $value) {
				$value['url'] = url('Question/detail', ['ask_id' => $value['ask_id']]);
				$arr[] = $value;
			}
			$data['list'] = $arr;
			$this->success("获取成功", '', $data);
		}
	}

}




